﻿using System;
using System.Web.UI.WebControls;
using Model;
using Model.Entities;
using MusicSite.ViewClasses;

namespace MusicSite.Views.Content.Album
{
    public partial class EditAlb : BasePage
    {
        private AlbumProvider albProv = new AlbumProvider();
        private AlbumDetails editAlb = new AlbumDetails();
        protected void Page_Load(object sender, EventArgs e)
        {
            CheckAuthenticationEditPage();

            string albId = Request.QueryString["AlbumId"];
            if (string.IsNullOrEmpty(albId))
            {
                btnDelete.Visible = false;
                btnUpdate.Text = "Create";
                FillDropDownList();
                return;
            }
            editAlb = albProv.GetAlbumById(Convert.ToInt32(albId));
            if (!IsPostBack)
            {
                albumName.Text = editAlb.AlbumName;
                releaseDate.Text = editAlb.ReleaseDate.Value.ToShortDateString();
                style.Text = editAlb.Style;
                cover.ImageUrl = "~/Content/images/" + editAlb.Cover;
                url.Text = editAlb.Url;
                FillDropDownList();
                artist.SelectedValue = editAlb.ArtistId.ToString();
            }
        }

        protected void FillDropDownList()
        {
            artist.DataSource = new ArtistProvider().GetArtists();
            artist.DataTextField = "ArtistName";
            artist.DataValueField = "ArtistId";
            artist.DataBind();
        }

        protected void UpdateAlbum(object sender, EventArgs e)
        {
            if (IsValid)
            {
                editAlb.AlbumName = albumName.Text;
                editAlb.ReleaseDate = DateTime.Parse(releaseDate.Text);
                editAlb.Style = style.Text;
                if (!string.IsNullOrEmpty(fileUpload.FileName))
                    editAlb.Cover = Guid.NewGuid() + fileUpload.FileName;
                SaveImageOnDisc(fileUpload);

                Uri newUrl;
                if (!Uri.TryCreate(url.Text, UriKind.Absolute, out newUrl))
                    editAlb.Url = "http://" + url.Text;
                else
                    editAlb.Url = newUrl.ToString();
                editAlb.ArtistId = Convert.ToInt32(artist.SelectedValue);
                if (btnUpdate.Text == "Create")
                    albProv.InsertAlbum(editAlb);
                else
                    albProv.UpdateAlbum(editAlb);
                Response.Redirect("~/Views/Content/Album/IndexAlb.aspx");
            }
        }

        protected void DeleteAlbum(object sender, EventArgs e)
        {
            albProv.DeleteAlbum(editAlb);
            Response.Redirect("~/Views/Content/Album/IndexAlb.aspx");
        }

        public void SaveImageOnDisc(FileUpload img)
        {
            if (string.IsNullOrEmpty(img.FileName) || img.FileBytes.Length == 0)
                return;
            string dir = Server.MapPath("~/Content/images/" + editAlb.Cover);
            img.SaveAs(dir);
        }

        protected void ValidateUrl(object sender, ServerValidateEventArgs args)
        {
            Uri addr;
            if (Uri.TryCreate(args.Value, UriKind.Absolute, out addr))
            {
                if (addr.Scheme == "")
                    args.IsValid = Uri.IsWellFormedUriString("http://" + args.Value, UriKind.Absolute);
                else
                    args.IsValid = Uri.IsWellFormedUriString(args.Value, UriKind.Absolute);
            }
            else
                args.IsValid = Uri.IsWellFormedUriString("http://" + args.Value, UriKind.Absolute);
        }
    }
}
